package com.jia.lidao.ui.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jia.lidao.R;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.RefreshStatusMessage;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.DealAuditOrderRequest;
import com.jia.lidao.network.http.request.impl.DealWithLoanRequest;
import com.jia.lidao.ui.BaseActivity;
import com.jia.lidao.widget.CommonTitleBar;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by jia on 17/12/23.
 */

public class FeedBackActivity extends BaseActivity {


    private int id;

    private int paymenttype;

    private CommonTitleBar titleBar;

    private TextView submitTxt;

    private EditText reasonEdt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        id=getIntent().getIntExtra("ID",0);
        paymenttype=getIntent().getIntExtra("pay",5);
        initView();
    }

    private void initView() {
        titleBar=(CommonTitleBar)findViewById(R.id.common_title_bar);
        submitTxt=(TextView)findViewById(R.id.submit_txt);
        reasonEdt=(EditText) findViewById(R.id.reason_edt);
        if(paymenttype==5){
            titleBar.setTitle("驳回订货单");
        }else {
            titleBar.setTitle("驳回农资贷");
        }

        submitTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(reasonEdt.getText().toString())) {
                    showToast("请输入驳回原因");
                    return;
                }

                if(paymenttype==5){
                    showDialog("");
                    BusinessManager.getInstance().dealWithOrderAudit(new DealAuditOrderRequest(id, -1, reasonEdt.getText().toString()), new MyCallback<Void>() {
                        @Override
                        public void onSuccess(Void result, String message) {
                            dismissDialog();
                            showToast("驳回成功");
                            EventBus.getDefault().post(new RefreshStatusMessage());
                            if(AuditActivity.instance!=null)
                                AuditActivity.instance.finish();
                            finish();
                        }

                        @Override
                        public void onError(String errorCode, String message) {
                            dismissDialog();
                            showToast(message);
                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public void onRefreshToken() {
                            UserUtils.getInstance().goToLogin(FeedBackActivity.this);
                        }
                    });
                }else {
                    showDialog("");
                    BusinessManager.getInstance().dealWithLoanAudit(new DealWithLoanRequest(id, -1, reasonEdt.getText().toString()), new MyCallback<Void>() {
                        @Override
                        public void onSuccess(Void result, String message) {
                            dismissDialog();
                            showToast("驳回成功");
                            EventBus.getDefault().post(new RefreshStatusMessage());
                            if(AuditActivity.instance!=null)
                                AuditActivity.instance.finish();
                            finish();
                        }

                        @Override
                        public void onError(String errorCode, String message) {
                            dismissDialog();
                            showToast(message);
                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public void onRefreshToken() {
                            UserUtils.getInstance().goToLogin(FeedBackActivity.this);
                        }
                    });
                }
            }
        });

    }
}
